package com.xwyzsn.rentcar.utils;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Base64;

@Data
@Component
public class JwtToken {
    final private String secret= "123456";
    public String createToken(String username,String password,String roles){
        String token = username+";"+password+";"+roles+";"+secret;
        // base64
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(token.getBytes());

    }
    public String[] parseToken(String token){
        Base64.Decoder decoder = Base64.getDecoder();
        String[] tokens = new String(decoder.decode(token)).split(";");
        if(tokens.length!=4){
            return null;
        }
        if(!tokens[3].equals(secret)){
            return null;
        }
        return tokens;
    }

}
